#include "usart.h"


uint8_t Usart1_Serial_RxData;
uint8_t Usart1_Serial_RxFlag;
uint8_t Usart3_Serial_RxData;
uint8_t Usart3_Serial_RxFlag;

uint16_t car_speed = 100;
void set_car_speed(){
	uint16_t speed_level;
	if (car_speed==100){
		speed_level = 10;
		OLED_ShowNum(58,20,speed_level,2,16,1);
	}
	else{
		if(car_speed==50){
			speed_level = 0;
		}
		else if(car_speed==55){
			speed_level = 1;
		}
		else if(car_speed==60){
			speed_level = 2;
		}
		else if(car_speed==65){
			speed_level = 3;
		}
		else if(car_speed==70){
			speed_level = 4;
		}
		else if(car_speed==75){
			speed_level = 5;
		}
			else if(car_speed==80){
			speed_level = 6;
		}
		else if(car_speed==85){
			speed_level = 7;
		}
		else if(car_speed==90){
			speed_level = 8;
		}
		else if(car_speed==95){
			speed_level = 9;
		}
		else if(car_speed==100){
			speed_level = 10;
		}
		OLED_ShowNum(58,20,speed_level,1,16,1);
	}
}


void USART_init(uint32_t bound)
{
    GPIO_InitTypeDef GPIO_InitStrue;
    USART_InitTypeDef USART_InitStrue;
	
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
		
		// usart串口引脚
		GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStrue);
	
		GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
	  GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStrue);

		USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	  USART_InitStrue.USART_Parity=USART_Parity_No;
    USART_InitStrue.USART_BaudRate=bound;
	  USART_InitStrue.USART_StopBits=USART_StopBits_1;
		USART_InitStrue.USART_WordLength=USART_WordLength_8b;
    USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_Init(USART1,&USART_InitStrue);
		
    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);
		
    USART_Cmd(USART1,ENABLE);
		
	
		
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); 
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
		
		// wifi引脚
		GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStrue);
	
		GPIO_InitStrue.GPIO_Pin=GPIO_Pin_11;
	  GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStrue);

		USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	  USART_InitStrue.USART_Parity=USART_Parity_No;
    USART_InitStrue.USART_BaudRate=bound;
	  USART_InitStrue.USART_StopBits=USART_StopBits_1;
		USART_InitStrue.USART_WordLength=USART_WordLength_8b;
    USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_Init(USART3,&USART_InitStrue);
		
    USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);
		
    USART_Cmd(USART3,ENABLE);

}




int fputc(int ch,FILE *f)
{
	USART_SendData(USART1,(uint16_t)ch);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
	return ch;

}

void Serial_SendByte(uint8_t Byte)
	{
    USART_SendData(USART1,Byte);
    while (USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待置1
}
	
void Serial3_SendByte(uint8_t Byte)
	{
    USART_SendData(USART3,Byte);
    while (USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);//等待置1
}

void Serial_SendArray(uint8_t* Array,uint16_t Length)
	{
    uint16_t i;
    for(i=0;i<Length;i++){
        Serial_SendByte(Array[i]);
    }
}

void Serial_SendString(char* String)
	{
    uint8_t i;
    for(i=0;String[i] != '\0';i++){
        Serial_SendByte(String[i]);
    }
}
	
void Serial3_SendString(char* String)
	{
    uint8_t i;
    for(i=0;String[i] != '\0';i++){
        Serial3_SendByte(String[i]);
    }
}

uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
		//次方函数X**Y
    uint32_t Result = 1;
    while(Y--){
        Result *= X;
    }
    return Result;
}
void Serial_SendNumer(uint32_t Number,uint8_t Length)
	{
    uint8_t i;
    for(i=0;i<Length;i++){
        Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+0x30);
    }
}


// 串口
char Str_Buff[10];
u8 str_len=0;
void USART1_IRQHandler()
{
    if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET)
    {	
		Str_Buff[str_len] = USART_ReceiveData(USART1);
		if(Str_Buff[str_len]=='#')//如果检测到结束标志‘#’
		{
			Str_Buff[str_len]= '\0';//手动给字符串添加‘\0’结束符
			str_len = -1;
			Delay_ms(1);
			if (strcmp((char *)Str_Buff,"up")== 0){
				Car_Up();
				}
		}
		else{
				str_len++;
		}
      //Usart1_Serial_RxData = USART_ReceiveData(USART1);//数据读取到模块的变量里
      USART_ClearITPendingBit(USART1,USART_IT_RXNE);
    }
}

// wifi 接收tcp服务器下发的命令
void USART3_IRQHandler()
{
    //判断标志位，如果置1说明收到数据，进入if
    if(USART_GetFlagStatus(USART3,USART_IT_RXNE) == SET)
    {
      Usart3_Serial_RxData = USART_ReceiveData(USART3);//数据读取到模块的变量里
			
			if (Usart3_Serial_RxData==50){
				// 红色加蓝色，紫色
				GPIO_SetBits(GPIOA, GPIO_Pin_11); // 红
				GPIO_ResetBits(GPIOA, GPIO_Pin_12); // 绿
				GPIO_SetBits(GPIOA, GPIO_Pin_15); // 蓝
				
				//前进 2
				Car_Up();

			}
			else if (Usart3_Serial_RxData==51){
				// 绿色加蓝色，青色
				GPIO_ResetBits(GPIOA, GPIO_Pin_11);
				GPIO_SetBits(GPIOA, GPIO_Pin_12);
				GPIO_SetBits(GPIOA, GPIO_Pin_15);
				
				//后退 3
				Car_Down();
			}
			else if (Usart3_Serial_RxData==52){
				GPIO_ResetBits(GPIOA, GPIO_Pin_11);
				GPIO_SetBits(GPIOA, GPIO_Pin_12); // 绿色
				GPIO_ResetBits(GPIOA, GPIO_Pin_15);
				
				//左转 4
				Car_TurnLeft();
				
			}
			else if (Usart3_Serial_RxData==53){
				GPIO_ResetBits(GPIOA, GPIO_Pin_11);
				GPIO_ResetBits(GPIOA, GPIO_Pin_12);
				GPIO_SetBits(GPIOA, GPIO_Pin_15); // 蓝色
								
				//右转 5
				Car_TurnRight();
			}
			
			else if (Usart3_Serial_RxData==54){
				GPIO_SetBits(GPIOA, GPIO_Pin_11);
				GPIO_SetBits(GPIOA, GPIO_Pin_12);
				GPIO_SetBits(GPIOA, GPIO_Pin_15);
				//左旋转 6
				Car_Spin_L();
			}
			else if (Usart3_Serial_RxData==55){
				GPIO_SetBits(GPIOA, GPIO_Pin_11);
				GPIO_SetBits(GPIOA, GPIO_Pin_12);
				GPIO_ResetBits(GPIOA, GPIO_Pin_15);
				
				//右旋转 7
				Car_Spin_R();
			}
			
			
			else if (Usart3_Serial_RxData==56){
				GPIO_SetBits(GPIOA, GPIO_Pin_11);
				GPIO_ResetBits(GPIOA, GPIO_Pin_12);
				GPIO_ResetBits(GPIOA, GPIO_Pin_15);
				
				//停 8
				Car_Stop();
			}
			
			else if (Usart3_Serial_RxData==66){

				//加速 B
				if (car_speed<100){
				car_speed +=5;
				}
				PWM12_SetCompare1(car_speed);
				PWM12_SetCompare2(car_speed);

			}
			else if (Usart3_Serial_RxData==67){

				//减速 C
				if (car_speed>=55){
				car_speed -=5;
				}
				PWM12_SetCompare1(car_speed);
				PWM12_SetCompare2(car_speed);
			}
			
			USART_ClearITPendingBit(USART3,USART_IT_RXNE);
    }
}

